//
//  RDiscoverPageViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/24.
//

#import "RDiscoverPageViewController.h"
#import "RSRefreshTableView.h"
#import "RDiscoverTableViewCell.h"

#import "RDiscoverListRequest.h"

#import "RVideoDetailsViewController.h"

@interface RDiscoverPageViewController ()<RSRefreshTableViewDelegate,UITableViewDelegate,UITableViewDataSource>{
    TimeRange _dateType;
}

/** 列表*/
@property(nonatomic,strong)RSRefreshTableView* tableView;

@end

@implementation RDiscoverPageViewController

- (instancetype)init{
    if(self = [super init]){
        _dateType = TimeRange_Today;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupTableView];
    
    [self requestData:Refresh];
}


-(void)setupTableView{
    
    //列表
    RSRefreshTableView* tableView = [[RSRefreshTableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), SCREEN_HEIGHT - IPHONE_NAVIGATION_BAR_HEIGHT-IPHONE_TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];
    
    tableView.refreshEnabled = YES;
    tableView.loadMoreEnabled = YES;
    tableView.refreshDelegate = self;
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = UIColor.whiteColor;
    tableView.rowHeight = PX(157)+16;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.sectionFooterHeight = CGFLOAT_MIN;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, CGFLOAT_MIN)];
    [tableView registerClass:NSClassFromString(discoverCell) forCellReuseIdentifier:discoverCell];
    [self.view addSubview:tableView];
    _tableView = tableView;

}


#pragma mark -- public
- (void)reloadDataWithTimeRange:(TimeRange)timeRange{
    _timeRange = timeRange;
    [self requestData:Refresh];
}

#pragma mark -- request
// 刷新数据
- (void)requestData:(RefreshType)refreshType{
    
    NSUInteger pageIndex = refreshType == Refresh?1:self.tableView.pageIndex+1;
    
    RDiscoverListRequest* request = [RDiscoverListRequest new];
    request.c_id = self.VideoType;
    request.date_type = _timeRange;
    request.action = @"r_l";
    request.page = pageIndex;
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        [[response success:^(RDiscoverList*  _Nonnull data) {
            self.tableView.total = data.total;
            self.tableView.pageIndex = data.current_page;
            [self.tableView updateData:data.lists refreshType:refreshType isSuccess:YES];
        }] failure:^(APIError * _Nonnull error) {
            [self.tableView updateData:@[] refreshType:refreshType isSuccess:NO];
        }] ;
    }];

}

#pragma mark --UITableViewDelegate  UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tableView.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RVideoObj* movie = self.tableView.datas[indexPath.row];
    
    RDiscoverTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:discoverCell forIndexPath:indexPath];
    cell.index = indexPath.row;
    cell.movie = movie;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RVideoObj* movie = self.tableView.datas[indexPath.row];
    
    RVideoDetailsViewController* detailController = [RVideoDetailsViewController new];
    detailController.video = movie;
    [UIViewController showViewController:detailController animated:YES];
}

#pragma mark  --RSRefreshTableViewDelegate

- (void)tableView:(RSRefreshTableView *)tableView refreshType:(RefreshType)refreshType{
    [self requestData:refreshType];
}

@end
